package com.blog.common.core.context;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * <p>
 * 类扫描工具
 * </p>
 *
 * @author Valerie
 * @date 2023/5/20
 **/
@Slf4j
@Component
public class ClassScanner {
    /** 实体类set */
    private final static Set<Class<?>> CLASS_SET;

    /** domain 目录 */
    private static final String PACKAGE_NAME = "com.blog.common.domain";

    // 初始化
    static {
        Reflections reflections = new Reflections(PACKAGE_NAME, new SubTypesScanner(false));
        CLASS_SET = reflections.getSubTypesOf(Object.class);
    }

    /**
     * 判断class是否为实体类
     *
     * @param clazz 实体类
     * @return {@link boolean}
     */
    public static boolean isEntity(Class<?> clazz) throws ClassNotFoundException {
        if (CollectionUtil.isNotEmpty(CLASS_SET)){
            return CLASS_SET.contains(clazz);
        }
        throw new ClassNotFoundException();
    }
}
